package com.macro.mall.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.dto.BmsInterfaceConfigParam;
import com.macro.mall.model.BmsInterfaceConfig;
import com.macro.mall.service.BmsInterfaceConfigService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * 接口配置
 * Created 2024/8/12.
 *
 */
@Controller
@Api(tags = "BmsInterfaceConfig")
@Tag(name = "BmsInterfaceConfig", description = "接口配置")
@RequestMapping("/bmsInterfaceConfig")
public class BmsInterfaceConfigController {
    private static final String CODE_EXIST_MESSAGE = "CODE已存在";
    
    @Autowired
    private BmsInterfaceConfigService bmsInterfaceConfigService;
    
    @ApiOperation("添加接口配置")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<?> create(@RequestBody BmsInterfaceConfigParam bmsInterfaceConfigParam) {
        Long id = bmsInterfaceConfigParam.getId();
        
        BmsInterfaceConfig bmsInterfaceConfig = bmsInterfaceConfigService.getItem(id);
        
        if (bmsInterfaceConfig == null) {
            int count = bmsInterfaceConfigService.create(bmsInterfaceConfigParam);
            if (count > 0) {
                return CommonResult.success(count);
            } else {
                return CommonResult.failed();
            }
        } else {
            return CommonResult.failed(CODE_EXIST_MESSAGE);
        }
    }
    
    @ApiOperation("修改接口配置")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<?> update(@RequestBody BmsInterfaceConfigParam bmsInterfaceConfigParam) {
        int count = bmsInterfaceConfigService.update(bmsInterfaceConfigParam);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }
    
    @ApiOperation("分页接口配置")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPage<BmsInterfaceConfig>> getList(@RequestBody BmsInterfaceConfigParam bmsInterfaceConfigParam) {
        List<BmsInterfaceConfig> bmsInterfaceConfigList = bmsInterfaceConfigService.getList(bmsInterfaceConfigParam);
        return CommonResult.success(CommonPage.restPage(bmsInterfaceConfigList));
    }
    
    @ApiOperation("根据id获取接口配置")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<BmsInterfaceConfig> getItem(@PathVariable Long id) {
        BmsInterfaceConfig bmsInterfaceConfig = bmsInterfaceConfigService.getItem(id);
        return CommonResult.success(bmsInterfaceConfig);
    }
    
    @ApiOperation("删除接口配置")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<?> delete(@RequestParam("ids") List<Long> ids) {
        int count = bmsInterfaceConfigService.delete(ids);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }
}
